<template>
  <div>
    <div class="taskTitle">当前活跃进程</div>
    <el-table :data="tableData">
      <el-table-column label="名称" prop="title"/>
      <el-table-column label="CPU" prop="cpu"/>
      <el-table-column label="内存" prop="ram"/>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "Process",
  data: () => ({
    randomSeed: 1,
  }),
  computed:{
    tableData() {
      let list = this.$store.state.windowList
      // cpu总和
      let cpu = list.reduce((previousValue, currentValue) => previousValue + currentValue.meta.cpu, 0)
      let percent = list.length / this.$store.state.config.maxProcess
      return this.$store.state.windowList.map(item => ({
        ...item,
        cpu: (item.meta.cpu * (0.8 + 0.2 * Math.random()) / cpu * percent * this.randomSeed * 100).toFixed(2) + "%",
        ram: (item.meta.cpu * (0.8 + 0.2 * Math.random()) / cpu * percent * this.randomSeed * 2000).toFixed(2) + "MB"
      }))
    }
  },
  mounted() {
    this.processRandomSeed(0.8, 1)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods:{
    processRandomSeed(min, max, interval) {
      this.timer = setInterval(()=>{
        this.randomSeed = min + (max-min) * Math.random()
      }, interval || 1000)
    }
  }
}
</script>

<style scoped>

</style>
